<?php

class FlickrDatabase {
    var $HOST = "database_host";
    var $USERNAME = "database_username";
    var $USERGROUP = "flickr_group";
    var $PASSWORD = "database_password";
    var $FLICKR_DB = "database_name";

    var $CACHE_SECONDS = 604800; // Una semana
    //var $CACHE_SECONDS = 86400; // Un día
    var $API_KEY = "flickr_api_key";
    var $API_SECRET = "flickr_api_secret";

    var $dblink;
    var $tag;
    var $start_date;
    var $end_date;
    var $sql;
    var $result;
    var $connectionString;
    var $today;

    function FlickrDatabase() {
        $this->connectionString = "mysql://" . $this->USERNAME . ":" . 
            $this->PASSWORD . "@" . $this->HOST . "/flickr";

        $this->dblink = mysql_connect($this->HOST, $this->USERNAME, $this->PASSWORD);
        mysql_select_db($this->FLICKR_DB, $this->dblink);

        // Select the last tag by end date
        $this->sql = "SELECT `tag`, `start_date`, `end_date` FROM `configuracion` ORDER BY `end_date` DESC LIMIT 1";
        $this->result = mysql_query($this->sql, $this->dblink);
        $row = mysql_fetch_row($this->result);
        $this->tag = $row[0];
        $this->start_date = $row[1];
        $this->end_date = $row[2];
        $this->today = date("Y-m-d");

        $this->createTableIfNotExist();
    }

    function getTag() {
        return $this->tag;
    }

    function getVotos($photoid) {
        $this->sql = "SELECT SUM(`puntos`) FROM `" .
            $this->tag . "` WHERE `foto_id` LIKE '" .
            $photoid . "'";
        $this->result = mysql_query($this->sql, $this->dblink);
        $row = mysql_fetch_row($this->result);
        $votos = $row[0];
        if ($votos == "") {
            $votos = 0;
        }
        return $votos;
    }

    function checkVotada($photoid, $user) {
        $this->sql = "SELECT `foto_id`, `usuario` FROM `" .
            $this->tag . "` WHERE `foto_id` LIKE '" .
            $photoid . "' AND `usuario` LIKE '" . $user . "'";
        $this->result = mysql_query($this->sql, $this->dblink);
        return (mysql_num_rows($this->result) != 0);
    }

    function puntosUsados($user, $points) {
        $this->sql = "SELECT `usuario`, `puntos` FROM `" .
            $this->tag . "` WHERE `usuario` LIKE '" .
            $user . "' AND puntos=" . $points;
        $this->result = mysql_query($this->sql, $this->dblink);
        return (mysql_num_rows($this->result) != 0);
    }

    function setPuntos($photoid, $user, $points) {
        $this->sql = "INSERT INTO `" . $this->tag .
            "` SET foto_id='" . $photoid . "', usuario='" .
            $user . "', puntos=" . $points;
        $this->result = mysql_query($this->sql, $this->dblink);
    }

    function getVotantes() {
        $i = 0;
        $users = array();
        $this->sql = "SELECT DISTINCT `usuario` FROM `" .
            $this->tag . "`";
        $this->result = mysql_query($this->sql, $this->dblink);
        if (mysql_num_rows($this->result) != 0) {
            while ($row = mysql_fetch_row($this->result)) {
                $users[$i] = $row[0];
                $i++;
            }
        }
        return $users;
    }

    function createTableIfNotExist() {
        $this->sql = "
            CREATE TABLE IF NOT EXISTS `" . $this->tag . "` (
                `id` int(10) unsigned NOT NULL auto_increment,
                `foto_id` varchar(512) collate utf8_unicode_ci NOT NULL,
                `usuario` varchar(512) collate utf8_unicode_ci NOT NULL,
                `puntos` tinyint(3) unsigned NOT NULL default '0',
                PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
        $this->result = mysql_query($this->sql, $this->dblink);
    }

    function isOnDate() {
        return (($this->start_date <= $this->today)
            and ($this->end_date >= $this->today));
    }

    function close() {
        mysql_close($this->dblink);
    }
}
?>
